package com.riant.aop;

import org.apache.commons.codec.binary.Hex;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class TestSHA {
    private static final String SHA1="SHA-1";
    private static final String SHA256="SHA-256";
    private static final String SHA512="SHA-512";
    public static void main(String[] args) {
        testSHA("hello,world，张三");
        testSHA256("hello,world，张三");
        testSHA512("hello,world，张三");
    }

    //
    public static String testSHA(String message){
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(SHA1);
            messageDigest.update(message.getBytes());
            byte[] bytes = messageDigest.digest();
            System.out.println(bytes.length);
            String result = Hex.encodeHexString(bytes);
            System.out.println("sha1:"+result);
            return  result;


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String testSHA256(String message){
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(SHA256);
            messageDigest.update(message.getBytes());
            byte[] bytes = messageDigest.digest();
            System.out.println(bytes.length);
            String result = Hex.encodeHexString(bytes);
            System.out.println("sha256:"+result);
            return  result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String testSHA512(String message){
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(SHA512);
            messageDigest.update(message.getBytes());
            byte[] bytes = messageDigest.digest();
            System.out.println(bytes.length);
            String result = Hex.encodeHexString(bytes);
            System.out.println("sha256:"+result);
            return  result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}
